package my2Cents.tools;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.util.Vector;

import my2Cents.BodyState;
import my2Cents.Sandbox;
import my2Cents.Undo;
import my2Cents.Util;
import net.phys2d.math.Vector2f;
import net.phys2d.raw.Body;
import net.phys2d.raw.BodyList;
import net.phys2d.raw.StaticBody;

public class CopyTool extends MoverTool {
	private Body oldBody;
	private BodyState cBodyState;
	private float startAngle = 0;
	private int stage = -1;
	private Vector2f startPos;
	private Vector2f shapePos;
	private double initAngle;
	
	public CopyTool() {
		setGhostColor(AbstractPhysTool.GHOSTCOLOR_COPY);
		setDescription(
				"Click on an object and drag to\n" +
				"make a copy of it."
			);
	}
	
	public void startTool(MouseEvent arg0) {
		if(stage == 0) {
			stage = -1;
		} 
		if(stage == -1) {
			super.startTool(arg0);
			stage++;
		}
		if(stage == 0) {
			if(currentBody != null) {
				oldBody = currentBody;
				initAngle = oldBody.getRotation();
				BodyState s = new BodyState(currentBody);
				currentBody = s.getNewBody();
				getWorld().clearArbiters(currentBody);
				getWorld().add(currentBody);
				currentBody.addExcludedBody(Util.getHandle());
				oldBody.addExcludedBody(Util.getHandle());
				currentBody.addExcludedBody(oldBody);
				cBodyState = new BodyState(currentBody);
				getWorld().clearArbiters(cBodyState.getBody());
				for(int i=0; i<getWorld().getBodies().size(); i++) {
					Body b = getWorld().getBodies().get(i);
					if(!(b.isStatic())) {
					oldBody.addExcludedBody(b);
					}
				}
				startAngle = oldBody.getRotation();
				Util.clearBodyCollisions(Util.getHandle(), getWorld());

				String shape = oldBody.getShape().getName();
				if(!sandbox.isRunning()) {
					Sandbox.getUndoList().add(new Undo(currentBody, getWorld(), shape));
				} else {
					Sandbox.getUndoList().add(new Undo(currentBody, getWorld(), shape));
					Sandbox.getUndoList().add(new Undo(oldBody, getWorld(), shape));
				}
			}
		}
	}
	
	public void toolThink(MouseEvent arg0) {
		if(stage == 0) super.toolThink(arg0);
		if(currentBody != null && oldBody != null) {
			if(cBodyState != null && 
					Sandbox.isRunning() && 
					!(currentBody.isStatic())) {
				cBodyState.resolveBody(currentBody);
			}
			if(stage == 0) {
				oldBody.setRotation(startAngle);
			}
			//currentBody.setVelocity(new Vector2f(0,0));
		}
		if(stage == 1) {
			Vector2f vec = new Vector2f(arg0.getX()+3, arg0.getY());			
			Vector2f vec2 = new Vector2f(shapePos);
			double temp = Math.toDegrees(Util.angleVec(vec2, vec));
			double temp2 = temp + initAngle;
			if(Sandbox.isRunning()) {
				oldBody.setRotation((float) (temp2/57.3));
			} else {
				currentBody.setRotation((float) (temp2/57.3));
			}
		}
	}
	
	public void endTool(MouseEvent arg0) {
		if(oldBody != null && currentBody != null) {
			if(stage == 0) {
				stage++;
				if(Sandbox.isRunning()) {
					shapePos = (Vector2f) oldBody.getPosition();
				} else {
					shapePos = (Vector2f) currentBody.getPosition();
				}
				for(int i=0; i<getWorld().getBodies().size(); i++) {
					Body b = getWorld().getBodies().get(i);
					if(!(b.isStatic())) {
						oldBody.removeExcludedBody(b);
					}
				}
				currentBody.removeExcludedBody(oldBody);
				if(!(currentBody.isStatic())) {
					currentBody.setMoveable(true);
				}
				//getWorld().step();
				if(Util.getHandle() != null && oldBody != null && joint != null) {
					joint.set(Util.getHandle(), oldBody, (Vector2f) oldBody.getPosition());
				}
				endTool(arg0);
				//startTool(arg0);
				initAngle = oldBody.getRotation();
			} else if(stage == 1) {
				oldBody.setAngularVelocity(0);
				oldBody.setVelocity(new Vector2f(0,0));
				super.endTool(arg0);
				stage = -1;
			}
		} else if(stage == -1) {
			super.endTool(arg0);
		}
	}

}
